The present invention relates to asynchronous calls in programs, and more specifically, to a method and apparatus for controlling asynchronous calls.
Asynchronous calls are used in many programs. When conducting an asynchronous call, the main thread of the caller does not need to wait for the execution of the called function, but continues executing the subsequent code. Once the callee finishes executing the called function, it in return calls the original caller to return the result, which is referred to as callback. Accordingly, the main thread obtains the execution result via the callback function. However, one major problem of the asynchronous call is that the call return time is not predetermined. Therefore, in the case of a plurality of asynchronous calls, the execution order of the corresponding callback functions cannot be predetermined. This adds difficulty to the testing and debugging of programs.
FIG. 1 shows the execution flow of a program comprising asynchronous calls. In the example of FIG. 1, the main program successively executes two asynchronous calls, AS Call 1 and AS Call 2, and then continues executing other functions, such as synchronous call functions SC1, SC2, etc. For the two asynchronous call functions, developers may expect to obtain returned results of the two calls in sequence, for example, obtaining the callback of AS Call 1 between SC1 and SC2, and obtaining the callback of AS Call 2 after SC2. However, depending on the execution of AS Call 1 and AS Call 2 by the callee, the execution order of the corresponding callback functions may be changed. For example, in certain cases, the return time of AS Call 2 may be earlier than that of AS Call 1 (as shown by the dashed lines in FIG. 1). In this case, the execution result of the main program may be different from the expected result of sequential return, thus causing errors. As these errors only occur in a certain return order of asynchronous calls, it is very difficult to find them in a conventional program debugging process.
As the return time of the asynchronous calls cannot be controlled, in order to conduct debugging, developers usually have to execute the program a large number of times, trying to reproduce the asynchronous call return order that causes errors. However, the procedure is time consuming, and still cannot guarantee a debugging result.
In another solution, developers extract the asynchronous call-related logic from the program, and test it. For example, suppose there are 5 asynchronous calls in a program, and a certain return order would cause errors in the program. The developers need to spend a lot of time to generate permutations of all possible return orders, and find among them the order that causes errors. In addition, with the rapid development of various program frameworks, it is very difficult to extract a single logical operation from complex program functional blocks.
Therefore, it is desired to be able to control the return of the asynchronous calls, thereby facilitating the testing and debugging of programs.